package view;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListCellRenderer;
import javax.swing.WindowConstants;

import controller.Sistema;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class AltaPrendaTemporada extends javax.swing.JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	protected static final Component IngresarCant = null;
	private JLabel labelTitulo;
	private JLabel labelNombre;
	private JLabel labelStock;
	private JTextField campoStock;
	private JTextField campoNombre;
	private JScrollPane scrollMateriales;
	private JComboBox<String> comboEstaciones;
	private JButton botonCancelar;
	private JList<MaterialView> listaMateriales;
	private JLabel labelMensajeMateriales;
	private JButton botonAceptar;
	private JLabel estacion;
	private JOptionPane MensajeConfirmacion;
	private JOptionPane MensajeError;

	public AltaPrendaTemporada() {
		super();
		initGUI();
	}
	
	class CellRendererMaterialView extends JLabel implements ListCellRenderer<MaterialView>{
		
		private static final long serialVersionUID = 1L;
		public CellRendererMaterialView() {
			setOpaque(true);
		  }
		
		public Component getListCellRendererComponent(JList<? extends MaterialView> list, MaterialView value, 
				int index, boolean isSelected, boolean cellHasFocus) {
			  
			this.setText(value.getNombre());
		    if (isSelected) {
		        setBackground(list.getSelectionBackground());
		        setForeground(list.getSelectionForeground());
		      } else {
		        setBackground(list.getBackground());
		        setForeground(list.getForeground());
		      }
			return this;
		  }
	}
	
	private void initGUI() {
		try {
			this.setSize(420, 471);
			setLocationRelativeTo(null);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			{
				labelTitulo = new JLabel();
				this.add(labelTitulo);
				labelTitulo.setText("Alta de Prenda de Temporada");
				labelTitulo.setBounds(12, 9, 211, 27);
				labelTitulo.setFont(new java.awt.Font("Segoe UI",0,16));
			}
			{
				labelNombre = new JLabel();
				this.add(labelNombre);
				labelNombre.setText("Nombre");
				labelNombre.setBounds(12, 49, 103, 26);
			}
			{
				campoNombre = new JTextField();
				this.add(campoNombre);
				campoNombre.setBounds(134, 50, 137, 25);
			}
			{
				labelStock = new JLabel();
				getContentPane().add(labelStock);
				labelStock.setText("Stock");
				labelStock.setBounds(12, 90, 104, 25);
			}
			{
				campoStock = new JTextField();
				getContentPane().add(campoStock);
				campoStock.setBounds(134, 90, 136, 25);
			}
			{
				estacion = new JLabel();
				this.add(estacion);
				estacion.setText("Estacion");
				estacion.setBounds(12, 134, 99, 20);
			}
			{
				ComboBoxModel<String> jComboBox1Model = 
					new DefaultComboBoxModel<String>(
							new String[] { "Primavera", "Verano", "Otono", "Invierno" });
				comboEstaciones = new JComboBox<String>();
				this.add(comboEstaciones);
				comboEstaciones.setModel(jComboBox1Model);
				comboEstaciones.setBounds(134, 133, 137, 23);
			}
			{
				labelMensajeMateriales = new JLabel();
				getContentPane().add(labelMensajeMateriales);
				labelMensajeMateriales.setText("Materiales (mantener CTRL para seleccion multiple):");
				labelMensajeMateriales.setBounds(21, 181, 330, 16);
			}
			{
				scrollMateriales = new JScrollPane();
				getContentPane().add(scrollMateriales);
				scrollMateriales.setBounds(21, 209, 354, 143);
				{
					Vector<MaterialView> mats = Sistema.getInstance().obtenerVistaMateriales();
					listaMateriales = new JList<MaterialView>(mats);
					scrollMateriales.setViewportView(listaMateriales);
					ListCellRenderer<MaterialView> renderer = new CellRendererMaterialView();
					listaMateriales.setCellRenderer(renderer);
					listaMateriales.setBounds(scrollMateriales.getBounds());
					listaMateriales.setPreferredSize(scrollMateriales.getSize());
				}
			}
			{
				botonCancelar = new JButton();
				getContentPane().add(botonCancelar);
				botonCancelar.setText("Cancelar");
				botonCancelar.setBounds(79, 370, 109, 37);
				botonCancelar.setFont(new java.awt.Font("Segoe UI",0,16));
				botonCancelar.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e) {
						close();
					}
				});
			}
			{
				botonAceptar = new JButton();
				this.add(botonAceptar);
				botonAceptar.setText("Aceptar");
				botonAceptar.setBounds(239, 370, 95, 37);
				botonAceptar.setFont(new java.awt.Font("Segoe UI",0,16));
				botonAceptar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						try{
							Vector<ItemMaterialView> items = new Vector<ItemMaterialView>();
							List<MaterialView> materiales = listaMateriales.getSelectedValuesList();
							for(MaterialView m: materiales){
								String cantidad = JOptionPane.showInputDialog(IngresarCant,"Ingrese una cantidad para el material "+m.getNombre()+" :");
								ItemMaterialView i = new ItemMaterialView(
										Integer.valueOf(cantidad),
										m.getCodigo()
										);
								items.add(i);
							}
							String estacion = (String) comboEstaciones.getSelectedItem();
							int codigo = Sistema.getInstance().insertarPrendaTemporada(
									campoNombre.getText(),
									estacion,
									Integer.valueOf(campoStock.getText()),
									items
									);
							if (codigo > 0){
								JOptionPane.showMessageDialog(MensajeConfirmacion, "La prenda se ha creado satisfactoriamente");
								setVisible(false);
							}else{
								JOptionPane.showMessageDialog(MensajeError, "Ya existe una prenda con ese nombre!");
							}
						} 
						catch (Exception e) {
							e.printStackTrace();
						}
					}
				});
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void close(){
		this.dispose();
	}
}

